package com.learn.xueer.ui.fragment

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.view.WindowManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.learn.xueer.app.appViewModel
import com.learn.xueer.app.base.BaseFragment1
import com.learn.xueer.app.util.CacheUtil
import com.learn.xueer.data.bindadapter.CustomBindAdapter
import com.learn.xueer.databinding.FragmentMineBinding
import com.learn.xueer.ui.activity.AcademicPerformanceActivity
import com.learn.xueer.ui.activity.ClearCacheActivity
import com.learn.xueer.ui.activity.ContactServerActivity
import com.learn.xueer.ui.activity.FeedBackActivity
import com.learn.xueer.ui.activity.MainActivity
import com.learn.xueer.ui.activity.MemberCenterActivity
import com.learn.xueer.ui.activity.PersonDataActivity
import com.learn.xueer.ui.activity.QuestionActivity
import com.learn.xueer.ui.activity.SetActivity
import com.learn.xueer.ui.activity.StudySetActivity
import com.learn.xueer.ui.viewmodel.MineViewModel
import kotlinx.android.synthetic.main.fragment_mine.sw
import me.hgj.jetpackmvvm.util.IS_EYE_CARE_OPEN
import me.hgj.jetpackmvvm.util.startActivity


class MineFragment : BaseFragment1<MineViewModel, FragmentMineBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mViewBind.click = ProxyClick()
        mViewBind.viewmodel = mViewModel
        var user = CacheUtil.getUser()
        mViewModel.userInfo.value = user
        mViewBind.tvVip.visibility = if (user?.vip_end_time == null) {
            View.INVISIBLE
        } else {
            View.VISIBLE
        }
        mViewBind.sw.isChecked = CacheUtil.isCareRye()
        CustomBindAdapter.circleImageUrl(
            mViewBind.ivAvatar,
            user?.avatar ?: ""
        )
        mViewModel.aboutReq()
        sw.setOnCheckedChangeListener { buttonView, isChecked ->
            if (buttonView.isPressed) {
                if (isChecked) {
                    (this.mActivity as MainActivity).openEye()
                    //开启
                    mViewModel.eyeReq("2") {
                        CacheUtil.setIsCareEye(true)
                    }
                } else {
                    (this.mActivity as MainActivity).closeEye()
                    mViewModel.eyeReq("1") {
                        CacheUtil.setIsCareEye(false)
                    }
                }
              IS_EYE_CARE_OPEN = isChecked
            }
        }
    }

    override fun lazyLoadData() {
        mViewModel.settingReq()
    }

    inner class ProxyClick {


        fun set() {
            startActivity(SetActivity::class)
        }

        fun userCenter() {
            startActivity(MemberCenterActivity::class)

        }

        fun score() {
            startActivity(AcademicPerformanceActivity::class)

        }

        fun userInfo() {
            startActivity(PersonDataActivity::class)

        }

        fun studySet() {
            startActivity(StudySetActivity::class)

        }

        fun clear() {
            startActivity(ClearCacheActivity::class)

        }

        fun question() {
            startActivity(QuestionActivity::class)

        }

        fun feed() {
            startActivity(FeedBackActivity::class)

        }

        fun contact() {
            startActivity(ContactServerActivity::class)

        }

    }

    override fun onResume() {
        super.onResume()
        var user = CacheUtil.getUser()
        mViewModel.userInfo.value = user
        this.mActivity.runOnUiThread {
            if (appViewModel.lccal != "") {
                CustomBindAdapter.circleImageUrl(
                    mViewBind.ivAvatar,
                    appViewModel.lccal
                )
            }

        }
    }

}